package com.changan.config;

import com.changan.controller.UserBackController;
import com.changan.model._MappingKit;
import com.jfinal.config.*;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSourceFactory;

public class JfinalConfig extends JFinalConfig {
    //读取属性文件
    static Prop p;

    public static DruidPlugin createDruidPlugin() {
        loadConfig();

        return new DruidPlugin(PropKit.get("jdbc.url"), PropKit.get("jdbc.username"), PropKit.get("jdbc.password"),PropKit.get("jdbc.driver"));
    }

    private static void loadConfig() {
        if(p==null){
            p = PropKit.use("config.properties");
        }
    }


    @Override
    public void configConstant(Constants me) {
        me.setViewType(ViewType.JSP);
        PropKit.use("config.properties");
        me.setDevMode(PropKit.getBoolean("system.devMode", false));
    }

    @Override
    public void configRoute(Routes me) {
        me.setBaseViewPath("/WEB-INF/pages");
        // /WEB-INF/pages/+login.jsp
        me.add("/", UserBackController.class,"/");
    }

    @Override
    public void configEngine(Engine engine) {

    }

    @Override
    public void configPlugin(Plugins plugins) {
        DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbc.url"), PropKit.get("jdbc.username"), PropKit.get("jdbc.password"),PropKit.get("jdbc.driver"));
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
        arp.addSqlTemplate("user.sql");
        arp.setShowSql(true);
        _MappingKit.mapping(arp);
        plugins.add(druidPlugin);
        plugins.add(arp);
    }

    @Override
    public void configInterceptor(Interceptors interceptors) {

    }

    @Override
    public void configHandler(Handlers handlers) {

    }
}
